/*
 * @Author: LC 1774939529@qq.com
 * @Date: 2023-11-05 17:49:32
 * @LastEditors: LC 1774939529@qq.com
 * @LastEditTime: 2023-11-12 13:31:54
 * @FilePath: \display_d_tube\RTL\digital_tube.v
 * @Description: 使用74来驱动显示数码管
 * 
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved. 
 */
//`include "./IC_74HC595_2023_11.v"

module digital_tube (
    input  clk_50Mhz, rst_n,
    output OE_n, shift_clock_cp, storage_clock_8div_cp, data_s 
);
    reg [7:0] data_disp = 8'b0; //输入到数码管所对应的io输出
    reg [4:0] data_value = 5'd0; //想要显示的数值
    reg [7:0] data_cs = 8'b0; //片选所对应的io输出
    reg [4:0] data_num = 5'd0; //想要选定的数码管，最大支持8个管

/*
显示数字“0”，abcdef亮，状态值00111111——>0x3f
显示数字“1”，bc亮，状态值00000110——>0x06
显示数字“2”，abdeg亮，状态值01011011——>0x5b
显示数字“3”，abcdg亮，状态值01001111——>0x4f
显示数字“4”，bcfg亮，状态值01100110——>0x66
显示数字“5”，acdfg亮，状态值01101101——>0x6d
显示数字“6”，acdefg亮，状态值01111101——>0x7d
显示数字“7”，abc亮，状态值00000111——>0x07
显示数字“8”，abcdefg亮，状态值01111111——>0x7f
显示数字“9”，abcdfg亮，状态值01101111——>0x6f
*/

    /* 转换成对应数码管的数值 */
    always @(posedge clk_50Mhz) begin
        if(rst_n == 1'b0) begin
            data_disp <= 8'b0;
            data_value <= 5'd0;
        end else begin
            case(data_value)
                5'd0: data_disp <= ~8'h3f;
                5'd1: data_disp <= ~8'h06;
                5'd2: data_disp <= ~8'h5b;
                5'd3: data_disp <= ~8'h4f;
                5'd4: data_disp <= ~8'h66;
                5'd5: data_disp <= ~8'h6d;
                5'd6: data_disp <= ~8'h7d;
                5'd7: data_disp <= ~8'h07;
                5'd8: data_disp <= ~8'h7f;
                5'd9: data_disp <= ~8'h6f;
                default: data_disp <= ~8'h00;
            endcase
        end
    end
    /* 转换成片选对应的io输出 */
    always @(posedge clk_50Mhz) begin
        if(rst_n == 1'b0) begin
            data_cs <= 8'b0;
            data_num <= 5'd0;
        end else begin
            case(data_num)
                5'd0: data_cs <= 8'h01;
                5'd1: data_cs <= 8'h02;
                5'd2: data_cs <= 8'h04;
                5'd3: data_cs <= 8'h08;
                5'd4: data_cs <= 8'h10;
                5'd5: data_cs <= 8'h20;
                5'd6: data_cs <= 8'h40;
                5'd7: data_cs <= 8'h80;
                default: data_cs <= 8'h00;
            endcase
        end
    end

    reg switch = 1'b0;
    reg [7:0] data = 1'b0;
    reg [2:0] count = 3'b0;
    reg flag = 1'b1;
    reg sel = 1'b0;

    /* 8计数器 */
    always @(posedge clk_50Mhz) begin
        if(rst_n == 1'b0)begin
            count <= 3'b0;
            flag <= 1'b1;
        end else begin
            if(count == 3'b111) begin
                count <= 3'b0;
                flag <= 1'b1;
            end else begin
                count <= count + 1'b1;
                flag <= 1'b0;
            end
        end
    end

    /* 根据flag依次填入片选跟显示的data */
    always @(posedge clk_50Mhz) begin
        if(rst_n == 1'b0)begin
            switch <= 1'b0;
            data <= 8'b0;
            sel <= 1'b0;
        end else begin
            if(flag == 1'b1) begin
                if(sel == 1'b0) begin //填入显示
                    data <= data_disp;
                    sel <= 1'b1;
                    switch <= 1'b0;
                end else begin //填入片选
                    data <= data_cs;
                    sel <= 1'b0;
                    switch <= 1'b1;
                end
            end
        end
    end

    IC_74HC595_2023_11 DT_6(
        .data                  (data                  ),
        .rst_n                 (rst_n                 ),
        .switch                (switch                ),
        .clk                   (clk_50Mhz             ),
        .OE_n                  (OE_n                  ),
        .MR_n                  (                      ),
        .shift_clock_cp        (shift_clock_cp        ),
        .storage_clock_8div_cp (storage_clock_8div_cp ),
        .data_s                (data_s                )
    );
                
endmodule

